package ph.com.gs3.formalistics.utilities;

import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;

/**
 * Utility for extracting data from the document view.
 * 
 * @author Ervinne Sodusta
 * 
 */
public class DocumentViewDataExtractor {

	public static final String TAG = DocumentViewDataExtractor.class.getSimpleName();

	public JSONObject extract(LinearLayout llDocumentFieldsContainer) {

		JSONObject data = new JSONObject();

		int fieldContainerCount = llDocumentFieldsContainer.getChildCount();

		// loop through all containers that contain 1 field/field group each
		for (int i = 0; i < fieldContainerCount; i++) {

			LinearLayout fieldContainer = (LinearLayout) llDocumentFieldsContainer.getChildAt(i);

			String containerId = (String) fieldContainer.getTag();

			if (containerId == null) {
				FLLogger.d(TAG, "Skipping one field container, it does not have an id/tag.");
				continue;
			}

			String fieldId = containerId.substring(0, containerId.length() - "_container".length());

			View fieldView = fieldContainer.findViewWithTag(fieldId);

			if (fieldView != null) {
				try {
					data.put(fieldId, extractDataFromField(fieldView));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				FLLogger.d(TAG, "No field found with id: " + fieldId);
			}

		}

		FLLogger.d(TAG, data.toString());

		return data;

	}

	protected Object extractDataFromField(View fieldView) {

		if (fieldView instanceof EditText) {
			return extractEditTextData((EditText) fieldView);
		} else if (fieldView instanceof RadioGroup) {
			return extractRadioButtonGroupData((RadioGroup) fieldView);
		} else if (fieldView instanceof LinearLayout) {
			// linear layout may contain a group of checkboxes
			return extractCheckboxGroupField((LinearLayout) fieldView);
		}

		return null;

	}

	protected String extractEditTextData(EditText etField) {

		if (etField.getText().toString() != null) {
			return etField.getText().toString();
		} else {
			return "";
		}

	}

	protected String extractRadioButtonGroupData(RadioGroup rgField) {

		int collectionChildCount = rgField.getChildCount();

		String value = "";

		// search which value is selected and return it
		for (int index = 0; index < collectionChildCount; index++) {

			if (rgField.getChildAt(index) instanceof RadioButton) {

				RadioButton rbField = (RadioButton) rgField.getChildAt(index);

				if (rbField.isChecked()) {
					value = rbField.getText().toString();
				}

			}

		}

		return value;
	}

	protected String extractDateField() {
		// DateField dateField = (DateField) currentField;
		// Date dateSelected = dateField.getdate();
		//
		// if (dateSelected != null) {
		// fieldValues.put(currentField.getTag().toString(),
		// Globals.SERVER_DATE_ONLY_FORMAT.format(dateSelected));
		// }
		return null;
	}

	protected String extractDateTimeField() {
		// DateTimeField dateTimeField = (DateTimeField) currentField;
		// Date dateSelected = dateTimeField.getdate();
		//
		// if (dateSelected != null) {
		// fieldValues.put(currentField.getTag().toString(),
		// Globals.SERVER_DATE_FORMAT.format(dateSelected));
		// }
		return null;
	}

	protected String extractSpinnerField(Spinner sField) {
		return (String) sField.getSelectedItem();
	}

	protected String extractCheckboxGroupField(LinearLayout llCheckboxGroup) {

		// if the view is a collection of check boxes, collect the
		// checked fields and add them up to a string comma separated

		int collectionChildCount = llCheckboxGroup.getChildCount();

		String value = "";

		for (int index = 0; index < collectionChildCount; index++) {

			if (llCheckboxGroup.getChildAt(index) instanceof CheckBox) {

				CheckBox cbField = (CheckBox) llCheckboxGroup.getChildAt(index);

				if (cbField.isChecked()) {
					value += cbField.getText().toString() + ",";
				}

			}

		}

		return value;

	}
}
